package com.ruoyi.system.netty;

import cn.hutool.core.io.BufferUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.socket.nio.NioClient;

import java.nio.ByteBuffer;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

public class Client {
    public static void main(String[] args) throws InterruptedException {
        //NioClient client = new NioClient("139.224.246.232", 42834);
        for (int i = 0; i < 1; i++) {
            send(i);
        }

        //测试粘包、半包情况
//        client.write(BufferUtil.createUtf8("LNG = 117.15618134, LAT = 34.15148926, TIME = 2022-04-15 09:50:01"));
//        Thread.sleep(5000);
//        client.write(BufferUtil.createUtf8("LNG = 117.15618134, LAT = 34.15148926, TIME = 2022-04-15 09:50:02"));
//        Thread.sleep(5000);
//        client.write(BufferUtil.createUtf8("LNG = 117.15618134, LAT = 34.15148926, TIME = 2022-04-15 09:50:03"));
//
//        String res = "fe010135ffd4054e4e353621521651fdfcfe";
//        byte[] bytes1 = HexUtil.decodeHex(res);
//        client.write(BufferUtil.create(bytes1));
//        Thread.sleep(5000);
//
//        res = "010135ffd4054e4e353621521652fdfc";
//        bytes1 = HexUtil.decodeHex(res);
//        client.write(BufferUtil.create(bytes1));
//        Thread.sleep(5000);
//
//        res = "fe010135ffd4054e4e353621521653fdfc";
//        bytes1 = HexUtil.decodeHex(res);
//        client.write(BufferUtil.create(bytes1));
    }

    public static void send(int i){
//        NioClient client = new NioClient("139.224.246.232", 42834);
//        client.write(BufferUtil.createUtf8("LNG = 117.15618134, LAT = 34.15148926, TIME = 2022-04-15 09:50:03"));
//        client.close();
        Thread thread=new Thread(()->{
            NioClient client = new NioClient("139.224.246.232", 42834);
            client.setChannelHandler((sc)->{
                ByteBuffer readBuffer = ByteBuffer.allocate(1024);
                //从channel读数据到缓冲区
                int readBytes = sc.read(readBuffer);
                if (readBytes > 0) {
                    //Flips this buffer.  The limit is set to the current position and then
                    // the position is set to zero，就是表示要从起始位置开始读取数据
                    readBuffer.flip();
                    //returns the number of elements between the current position and the  limit.
                    // 要读取的字节长度
                    byte[] bytes = new byte[readBuffer.remaining()];
                    //将缓冲区的数据读到bytes数组
                    readBuffer.get(bytes);
                    String body = HexUtil.encodeHexStr(bytes);
                    Console.log("[{}]: {}", sc.getRemoteAddress(), body);
                } else if (readBytes < 0) {
                    sc.close();
                }
            });
            client.listen();
            boolean s=true;
            int k = 0;
            while (true) {
                k++;
                if(k<-1){
                    try {
                        Thread.sleep(600000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }else{
                    if(s){
                        client.write(BufferUtil.createUtf8("LNG = 117.15618134, LAT = 34.15148926, TIME = 2022-04-15 09:50:03"));
                    }else{
                        String res = "fe010135ffd4054e4e35362152165"+i+"fdfc";
                        byte[] bytes1 = HexUtil.decodeHex(res);
                        client.write(BufferUtil.create(bytes1));
                    }
                    s=!s;
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }

        });
        thread.start();

    }

}

